package com.ithei.多线程第二天.并发工具类;

import java.util.concurrent.Semaphore;

public class DemoSemaphore {
    public static void main(String[] args) {
        Semaphore sem = new Semaphore(2);
        System.out.println("====宁夏路与兰州路红绿灯====");
        while (true) {
            for (int i = 1; i <= 2; i++) {
                int y = i;
                new Thread(() -> {
                    try {
                        if (y == 1) {
                            sem.acquire();
                            System.out.println("宁夏路路灯通行...........");
                            System.out.println("宁夏路南北边右拐15秒");
                            System.out.println("宁夏路南北边执行20秒");
                            Thread.sleep(5000);
                            Thread.sleep(15000);
                            System.out.println("宁夏路红灯不允许通行...........");
                            sem.release();
                        } else {
                            sem.acquire();
                            System.out.println("兰州路路灯通行...........");
                            System.out.println("兰州路东西边右拐15秒");
                            System.out.println("兰州路东西边执行20秒");
                            Thread.sleep(5000);
                            Thread.sleep(15000);
                            System.out.println("兰州路红灯不允许通行...........");
                            sem.release();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }


    }
}
